package com.w7vm.menuweb.dao;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.NoResultException;
import javax.persistence.PersistenceContext;

import com.w7vm.menuweb.entity.RestauranteFilialEnderecoWeb;
import com.w7vm.menuweb.entity.RestauranteFilialInfraestruturaWeb;
import com.w7vm.menuweb.entity.RestauranteFilialWeb;
import com.w7vm.menuweb.entity.RestauranteSedeWeb;

@Stateless
public class RestauranteFilialDAO extends GenericDAO<RestauranteFilialWeb>{

	@PersistenceContext
	private EntityManager em;
	
	public RestauranteFilialDAO() {
		super(RestauranteFilialWeb.class);
		// TODO Auto-generated constructor stub
	}
	
	public List<RestauranteFilialWeb> findFilialByIDMatriz(int idRestaurante){
		System.out.println("[IAM-EJB][findFilialByIDMatriz] :"+ idRestaurante);
		Map<String, Object> parameters = new HashMap<String, Object>();
		parameters.put("idRestaurante", idRestaurante);   
		return super.findByCriterion(RestauranteFilialWeb.FIND_FILIAL_BY_IDMATRIZ, parameters);
	}
	
	public RestauranteFilialWeb saveRestauranteFilial(RestauranteFilialWeb entity){
		em.persist(entity);
		return entity;
	}
	
	public RestauranteFilialInfraestruturaWeb saveInfraFilial(RestauranteFilialInfraestruturaWeb entity){
		em.persist(entity);
		return entity;
	}

	public RestauranteFilialEnderecoWeb saveEnderecoFilial(RestauranteFilialEnderecoWeb entity){
		em.persist(entity);
		return entity;
	}
	
	public RestauranteFilialWeb findFilialByID(int idRestaurante){
		System.out.println("[IAM-EJB][findFilialByID] :"+ idRestaurante);
		RestauranteFilialWeb objFilial = new RestauranteFilialWeb();
		objFilial = em.find(RestauranteFilialWeb.class, idRestaurante);
		return objFilial;
		
//		Map<String, Object> parameters = new HashMap<String, Object>();
//		parameters.put("idRestaurante", idRestaurante);   
//		return super.findOneResult(RestauranteFilialWeb.FIND_FILIAL_BY_ID, parameters);
	}
	
	public RestauranteFilialWeb mergeUpdate(RestauranteFilialWeb filialEntity){
		return super.update(filialEntity);
	}
	
	public List<RestauranteFilialWeb> findRestauranteFilialByCNPJ(String cnpj){
		System.out.println("[IAM-EJB][findRestauranteFilialByCNPJ] :"+ cnpj);
		Map<String, Object> parameters = new HashMap<String, Object>();
		parameters.put("cnpj", cnpj);   
		try{
			return super.findByCriterion(RestauranteFilialWeb.FIND_FILIAL_BY_CNPJ, parameters);
		}catch (NoResultException e){
			System.err.println(e.getMessage());
			return null;
		}
	}

}
